package logic

import (
	"context"
	"user-goods/userrpc/userrpc"

	"user-goods/api/internal/svc"
	"user-goods/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGoodAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodAddLogic {
	return &GoodAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GoodAddLogic) GoodAdd(req *types.GoodAddRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	add, err := l.svcCtx.UserRpc.GoodAdd(l.ctx, &userrpc.GoodAddRequest{
		GoodName: req.GoodName,
		Price:    float32(req.Price),
		Num:      req.Num,
		UserId:   req.UserId,
	})

	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Data:    nil,
			Code:    500,
		}, nil
	}

	return &types.Response{
		Message: "商品添加成功",
		Data:    add,
		Code:    200,
	}, nil
}
